package com.css.fxfzmhwz.base.user.repository.impl;

import com.css.fxfzmhwz.base.user.repository.CertificationRepository;
import com.css.fxfzmhwz.base.user.repository.entity.CertificationEntity;
import com.css.fxfzmhwz.constants.FxfzConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Repository
public class CertificationRepositoryImpl implements CertificationRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public CertificationEntity findById(String userId) {
        String sql = "select * from sys_certification where user_id = '"+ userId +"'";
        try {
            CertificationEntity certificationEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CertificationEntity.class));
            return certificationEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    @Transactional
    public void unlockLoginFail(String userId, String updateUser, Date updateTime) {
        String sql = " UPDATE sys_certification SET locked='"+ FxfzConstants.LOCKED_NORMAL +"',login_fail_num = 0,login_fail_time=NULL,update_user= '"+ updateUser +"' ,update_time = '"+ updateTime +"' WHERE user_id= '"+ userId +"'";
        jdbcTemplate.update(sql);
    }

    @Override
    @Transactional
    public void resetLoginFail(String userId, String updateUser, Date updateTime) {
        String sql = "UPDATE sys_certification SET login_fail_num = 0,login_fail_time=NULL,update_user= '"+ updateUser +"' ,update_time = '"+ updateTime +"' WHERE user_id = '"+ userId +"'";
        jdbcTemplate.update(sql);
    }

    @Override
    @Transactional
    public void updateLoginFailTime(String userId, String updateUser, Date updateTime) {
        String sql = "UPDATE sys_certification SET login_fail_time= '"+ updateTime +"',update_user= '"+ updateUser +"' ,update_time = '"+ updateTime +"' WHERE user_id = '"+ userId +"'";
        jdbcTemplate.update(sql);
    }

    @Override
    @Transactional
    public void loginFailLock(String userId, Date loginFailTime, String updateUser, Date updateTime) {
        String sql = "UPDATE sys_certification SET locked='"+FxfzConstants.LOCKED_TOO_MANY_FAILURES+"',login_fail_num = login_fail_num + 1,login_fail_time='"+ loginFailTime +"',update_user= '"+ updateUser +"' ,update_time = '"+ updateTime +"' WHERE user_id= '"+ userId +"'";
        jdbcTemplate.update(sql);
    }

    @Override
    @Transactional
    public void addLoginFailNum(String userId, String updateUser, Date updateTime) {
        String sql = "UPDATE sys_certification SET login_fail_num = login_fail_num + 1,update_user= '"+ updateUser +"' ,update_time = '"+ updateTime +"' WHERE user_id = '"+ userId +"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void save(CertificationEntity certification) {
        String sql = "insert into sys_certification values(:userId, :pwd, :salt, :locked, :loginFailNum, :loginFailTime, :isDefaultPwd," +
                " :pwdUpdateTime, :updateUser, :updateTime;)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(certification);
        namedParameterJdbcTemplate.update(sql, source);
    }
}
